(function () {
    let that;
    class Game {
        constructor(box) {
            this.box = box;
            this.food = new Food(this.box);
            this.snake = new Snake();
        }
        init() {
            this.food.render(this.box);
            this.snake.render(this.box);
            that = this;
        }
        run() {
            onKeyDown();

            this.timer = setInterval(function () {
                that.food.remove(that.box);
                that.snake.remove(that.box);

                that.snake.update();

                eat();

                that.food.render(that.box);
                that.snake.render(that.box);

                isGameOver();
            }, 100);
        }
    }
    function eat() {
        if (that.food.x === that.snake.items[0].x &&
            that.food.y == that.snake.items[0].y) {

            that.food.update(that.box);
            const last = {}
            Object.assign(last, that.snake.items[that.snake.items.length - 1])
            that.snake.items.push(last)

        }
    }
    function isGameOver() {
        console.log(that.snake.items[0].x);
        console.log(that.snake.items[0].y);
        const maxX = that.box.clientWidth / that.snake.width
        const maxY = that.box.clientHeight / that.snake.height
        // 撞墙
        if (that.snake.items[0].x < 0 || that.snake.items[0].x >= maxX || that.snake.items[0].y < 0 || that.snake.items[0].y >= maxY) {
            clearInterval(that.timer);
            
            alert('撞墙了！GAME OVER')
        }
        // 撞自己
        for (let i = 1, l = that.snake.items.length; i < l; i++) {
            if (that.snake.items[0].x === that.snake.items[i].x && that.snake.items[0].y === that.snake.items[i].y) {
                clearInterval(that.timer);
                alert('撞自己了！GAME OVER')
            }
        }
    }
    function onKeyDown() {
        // 键盘按下事件
        document.onkeydown = function (e) {
            const direction = that.snake.direction
            // 兼容写法
            e = e || window.event;
            // keycode 37 = Left ←
            // keycode 38 = Up ↑
            // keycode 39 = Right →
            // keycode 40 = Down ↓
            // 根据按下按键修改蛇头运动方向
            switch (e.keyCode) {
                case 37:
                    if (direction !== 'left' && direction !== 'right') {
                        that.snake.direction = 'left';
                    }
                    break;
                case 38:
                    if (direction !== 'up' && direction !== 'down') {
                        that.snake.direction = 'up';
                    }
                    break;
                case 39:
                    if (direction !== 'left' && direction !== 'right') {
                        that.snake.direction = 'right';
                    }
                    break;
                case 40:
                    if (direction !== 'up' && direction !== 'down') {
                        that.snake.direction = 'down';
                    }
                    break;
            }
        }
    }

    window.Game = Game;
}) ()